package com.itfly.controller;


import com.itfly.entity.EventCategories;
import com.itfly.entity.EventManagement;

import com.itfly.result.Result;
import com.itfly.service.EventCategoriesService;
import com.itfly.service.EventManagementService;
import com.itfly.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/management")
@CrossOrigin
public class EventManagementController {
    @Autowired
    private EventManagementService eventManagementService;
    //根据id查询事件分类
    @GetMapping("/list")
    public Result<List<EventManagement>> category(@RequestHeader(name = "Authorization") String token) {
        //根据用户名查询用户
//        System.out.println(token);
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("id");
        List<EventManagement> list = eventManagementService.findEventById(userId);
        return new Result<>(1,"查询成功",list);
    }
    //新增事件
    @PostMapping("/add")
    public Result<String> addCategory(@RequestHeader(name = "Authorization") String token,
                                      @RequestBody EventManagement eventManagement) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("id");
        eventManagement.setUserId(userId);
        System.out.println(eventManagement);
        eventManagementService.insertEvent(eventManagement);
        return new Result<>(1,"添加成功",null);
    }


    //查询分类
    @GetMapping("/search")
    public Result<EventManagement> Search(@RequestParam("id") Integer id) {
        EventManagement eventManagement = eventManagementService.selectByEventId(id);
        return new Result<>(1,"查询成功",eventManagement);
    }
    //更新分类
    @PutMapping("/update")
    public Result<String> updateCategory(@RequestBody EventManagement eventManagement) {
        //更新事件
        System.out.println(eventManagement);
        eventManagementService.updateCategory(eventManagement);
        return new Result<>(1,"修改成功",null);
    }
    //删除分类
    @RequestMapping("/delete")
    public Result<String> deleteCategory(@RequestParam("id")Integer id) {
        eventManagementService.deleteById(id);
        return new Result<>(1,"删除成功",null);
    }
    //模糊查询
    @PostMapping("/likeSearch")
    public Result<List<EventManagement>> searchCategory(@RequestHeader(name = "Authorization") String token,@RequestBody EventManagement eventManagement) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("id");
        eventManagement.setUserId(userId);
        List<EventManagement> eventManagements = eventManagementService.selectByLike(eventManagement);
//        System.out.println(eventManagements);
        return new Result<>(1,"查询成功",eventManagements);
    }
}
